% (1)
% •Input Signal: 61MHz Sine
% •Sampling Frequency: 80MHz
% •NCO Frequency: 20MHz
% This signal should be sampled, filtered and converted to a baseband I/Q-signal.
% Choose the parameters(ex: decimation rate) so that the DDC to baseband can be done in a simple way.
% Show the diagrams of your design including:
% •Input sampled signal time and frequency domain
% •NCO signal time and frequency domain
% •FIR filter speciation (Using Matlab Command : freqz)
% •Baseband signal time and frequency domain
clear; close all; clc;
f0 = 61e6;
fs = 3*80e6;
fnco = 60e6;
df = 1e6;
T = 1/df;
N = ceil(T*fs);
time = (0:N-1)/fs;
sInput = cos(2*pi*f0*time);
sNCO = [cos(2*pi*fnco*time); sin(2*pi*fnco*time)];
sModulated = sInput .* sNCO;
fBaseBand = abs(f0 - fnco - fs/2);
B = 1/T;
fCutOff = 50e6;
nOrder = 4096;
lowPassFilter = fir1(nOrder, fCutOff/fs*2);
sFilteredRe = filter(lowPassFilter, 1, sModulated(1,:));
sFilteredIm = filter(lowPassFilter, 1, sModulated(2,:));
sFiltered = sFilteredRe + 1j*sFilteredIm;

finput = 10e6;
filteredSignal = filter(lowPassFilter, 1, exp(2j*pi*finput*time);

% {{{ 画图
figure('Name', 'Input Sampled Signal', 'WindowStyle', 'docked');
subplot(211);
plotSignal(time, sInput, '.-', 'Time Domain', 'Time[s]', 'Amplitude[Voltage]');

sInputSpectrum = fftshift(fft(sInput));
frequency = (0:N-1)/N*fs - fs/2;
subplot(212);
plotSignal(frequency, db(sInputSpectrum), '-', 'Frequency Domain', 'Frequency[Hz]', 'Signal Spectrum');

figure('Name', 'Input NCO Signal', 'WindowStyle', 'docked');
subplot(211);
plotSignal(time, sNCO, '.-', 'Time Domain', 'Time[s]', 'Amplitude[Voltage]');
legend('I', 'Q');

sNCOSpectrum = abs(fftshift(fft(sNCO, [], 2), 2));
subplot(212);
plotSignal(frequency, db(sNCOSpectrum), '-', 'Frequency Domain', 'Frequency[Hz]', 'Signal Spectrum');

figure('Name', 'Modulated Signal', 'WindowStyle', 'docked');
subplot(211);
plotSignal(time, sModulated, '.-', 'Time Domain', 'Time[s]', 'Amplitude[Voltage]');
legend('I', 'Q');

sModulatedSpectrum = abs(fftshift(fft(sModulated, [], 2), 2));
subplot(212);
plotSignal(frequency, db(sModulatedSpectrum), '-', 'Frequency Domain', 'Frequency[Hz]', 'Signal Spectrum');

figure('Name', 'Filter', 'WindowStyle', 'docked');
[H, f] = freqz(lowPassFilter, 1, 2048, fs, 'whole');
index = [1025:2048, 1:1024];
f1 = [f(1025:2048) - fs; f(1:1024)];
plotSignal(f1, db(H(index)), '-', 'Filter Reponse', 'Frequency[Hz]', 'Impluse Reponse');

figure('Name', 'Filtered Signal', 'WindowStyle', 'docked');
subplot(211);
plotSignal(time, sFilteredRe, '.-', 'Time Domain', 'Time[s]', 'Amplitude[Voltage]');

filteredSpectrum = fftshift(fft(sFiltered, [], 2), 2);
subplot(212);
plotSignal(frequency, db(filteredSpectrum), '.-', 'Frequency Domain', 'Frequency[Hz]', 'Signal Spectrum');
% }}}

return;
decimalRate = 5;
timeDecimated = time(1:decimalRate:end);
sDecimated = sFiltered(:, 1:decimalRate:end);
figure('Name', 'Decimated Signal', 'WindowStyle', 'docked');
subplot(211);
plot(timeDecimated, sDecimated);
xlabel('Decimated Time[s]');
ylabel('Decimated Signal[Voltage]');
title('Time Domain');
legend('I', 'Q');
grid on;

sDecimatedSpectrum = abs(fftshift(fft(sDecimated, [], 2), 2));
M = N / decimalRate;
frequencyDecimated = (0:M-1) * fs / N - fs * M / (N * 2);
subplot(212);
plot(frequencyDecimated, sDecimatedSpectrum);
xlabel('Frequency[Hz]');
ylabel('Signal Spectrum[]');
title('Frequency Domain');
grid on;



% (2)
% •Input Signal: 60MHz IF frequency Chirp, Bandwidth 8MHz
% •Sampling Frequency: 80MHz
% •NCO Frequency: 20MHz
% This signal should be sampled, filtered and converted to a baseband I/Q-signal.
% The requirements are the same as (1). 
% Hints: Matlab commands — fir1, freqz, filter
